package com.jadefashion.service.impl;


import com.jadefashion.health.entity.PageResult;
import com.jadefashion.health.resources.MessageConstant;
import com.jadefashion.mapper.PermissionMapper;
import com.jadefashion.pojo.Permission;
import com.jadefashion.service.PermissionService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

//指定发布服务的接口
@Service(interfaceClass = PermissionService.class)
public class PermissionServiceImpl implements PermissionService {
    //注入mapper
    @Autowired
    private PermissionMapper permissionMapper;


    //TODO: 查询所有permission
    @Override
    public List<Permission> queryAllPermission() {
        return permissionMapper.queryAllPermission();
    }

    //TODO: 修改permission
    @Override
    public void editPermission(Permission permission) {
        permissionMapper.editPermission(permission);
    }

    //TODO: 删除权限
    @Override
    public void deletePermission(Integer id) {
        //查询此permission id是否被user引用
        Integer count = permissionMapper.queryPermissionIdForUserDetail(id);
        if (count > 0) {
        //存在即被引用,抛出异常
            throw new RuntimeException(MessageConstant.PERMISSION_IS_QUOTED);
        }
        permissionMapper.deletePermission(id);
    }

    //TODO: 添加权限
    @Override
    public void addPermission(Permission permission) {
        permissionMapper.addPermission(permission);
    }

    //TODO: 分页查询permission
    @Override
    public PageResult queryPage(String queryString, Integer currentPage, Integer pageSize) {

        //计算偏移量
        int offset = (currentPage - 1) * pageSize;
        //查询分页数据
        List<Permission> list_permission = permissionMapper.queryPageData(queryString, offset, pageSize);
        //查询总记录数
        Long totalCount = permissionMapper.queryTotalCount(queryString);
        //封装PageResult作为数据一并返回
        return new PageResult(totalCount, list_permission);
    }

    //TODO: 查询role对应permission
    @Override
    public List<Permission> queryPermissionForRole(Integer id) {
        return permissionMapper.queryPermissionForRoleId(id);
    }
}
